<?php
/**
 * periode/load-index.phtml
 * Chargement Ajax de la liste des périodes
 */
?>
<?php

use RHX\Helper\RHDateTime;
?>
<?php

$periodeList = $this->periodeList;
?>
<div class='list-container'>
    <table class="table table-striped list-table">
	<thead>
	    <tr>
		<td width='42%' style='text-align: center; font-weight: bold'>Libellé</td>
		<td width='25%' style='text-align: center; font-weight: bold'>Début</td>
		<td width='25%' style='text-align: center; font-weight: bold'>Fin</td>
		<td width='8%'>&nbsp;</td>
	    </tr>
	</thead>
	<tbody>
	    <?php $last		 = null; ?>
	    <?php foreach ($periodeList as $periode): ?>
    	    <tr>
    		<td style='text-indent: 10px'>
		    <a href='<?php echo $periode->libelle ?>' class='edit-libelle' data-pk='<?php echo $periode->id; ?>' data-name='libelle' data-type='text' onclick='return false;'><?php echo $periode->libelle ?></a>
    		</td>
    		<td style='text-align: center;'>
		    <a href='<?php
			$debut = new RHDateTime($periode->debut);
			echo $debut->date();
			?>' onclick='return false;'>
			    <?php echo $debut->dateFr(); ?>
		    </a>
    		</td>
    		<td style='text-align: center;'>
		    <a href='<?php
			$fin	 = new RHDateTime($periode->fin);
			echo $fin->date();
			?>' data-type='select' class='fin-<?php echo $periode->id; ?>' data-pk='<?php echo $periode->id; ?>' data-name='fin'>
			       <?php echo $fin->dateFr(); ?>
    		    </a>
    		</td>
    		<td style='text-align: center'>
    		</td>
	    </tr>
		<?php
		if (is_null($last)) {
		    $last = $periode;
		}
		?>
	    <?php endforeach; ?>
	</tbody>
    </table>
</div>

<script type='text/javascript'>
    $(function(){
	// Editable Libelle
	$('.edit-libelle').editable({
	    title : "Editer le libellé de la période",
	    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'periode', 'action'	 => 'editLibelle')) ?>'
	});
    <?php
    $now		 = new RHDateTime();
    $debut		 = new RHDateTime($last->debut);
    $fin		 = new RHDateTime($last->fin);
    $editable	 = false;
    $dateFinList	 = array();
    ?>
	    //alert("Derniere periode : <?php echo $last->debut; ?> et maintenant il est <?php echo $now->date(); ?>");
<?php
if ($now->isLT($debut)) {
	$editable	 = true;
	// Date de fin
	$begin		 = new \DateTime($last->debut);
    $begin->modify('next sunday');
    $end		 = new \DateTime($last->debut);
	// Incrémentation des jours
	for ($i = 0; $i < 10; $i++) {
	    $end->modify('next sunday');
	}
	$interval	 = \DateInterval::createFromDateString('1 week');
	$periodeDate	 = new \DatePeriod($begin, $interval, $end);
	foreach ($periodeDate as $date) {
	    $dateFr					 = new RHDateTime($date->format('Y-m-d'));
	    $dateFinList[$date->format('Y-m-d')]	 = $dateFr->dateFr();
	}
    }
    ?>
    <?php if ($editable): ?>
    		$('.fin-<?php echo $last->id ?>').editable({
    		    title: 'Edition de la date de fin',
            		    source: '<?php echo json_encode($dateFinList); ?>',
        			    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'periode', 'action'	 => 'changeLast')) ?>',
    			    success: function(response, newValue){
        				window.location.reload();
    			    }
    		});
<?php endif; ?>
    });
</script>